<?php $this->context->layout = 'form';?>
<?= \common\widgets\Asset::widget(['type'=>['select2','summernote','dragula']])?>

<form class="form-horizontal m" id="form-dictdata-add">
    <input type="hidden" name="id" value="<?=$info['id']?>">
    <div class="layui-tab layui-tab-brief" lay-filter="test1">
        <ul class="layui-tab-title" style="text-align: center">
            <?php foreach ($this->params['addAll'] as $key => $para): ?>
                <li class="<?= $key == 0 ? 'layui-this' : '' ?>" data-formname="<?= $para['form'] ?>"
                    lay-id="<?= $key ?>">
                    <?= $para['name'] ?>
                </li>
            <?php endforeach; ?>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show" data-id="0">
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|商品名称','extend'=>['name'=>'name','required'=>1,'info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name'=>'formselect|商品分类','extend'=>['name'=>'classify_id','required'=>1,'data'=>$this->params['classify']??[],'showvalue'=>'id','showname'=>'name','place'=>'','value'=>$info['classify_id']??'','class'=>'select2']])?>
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|标价','extend'=>['name'=>'price','type'=>'number','required'=>1,'info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|虚拟销量','extend'=>['name'=>'vr_sales','type'=>'number','value'=>0,'tips'=>' 外显销量 = 实际销量 + 虚拟销量','info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|简介','extend'=>['name'=>'intr','info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name' => 'image|商品主图', 'extend' => ['data'=>$info['main_img'],'link'=>'false','name' => 'imgarr1', 'id' => 'uploadimgbtn1', 'script_after' => '1', 'cat' => 'bmimage', 'water' => 'true', 'required' => 1, 'sm' => 3, 'tips' => '商品外显小图，建议尺寸：宽165px*高160px，jpg，jpeg，png格式']]) ?>
                <input type="hidden" name="main_img" id="img1" value="">
                <?= \common\widgets\FormInput::widget(['name' => 'image|详情页轮播图', 'extend' => ['data'=>$info['imgs'],'link'=>'false','name' => 'imgarr2', 'id' => 'uploadimgbtn2', 'multi' => 'true', 'drag' => 'true', 'cat' => 'bmimage', 'water' => 'true', 'required' => 1, 'sm' => 3, 'script_after' => '2', 'tips' => '商品详情页轮播图，可拖动排序，建议尺寸：宽375px*高275px，jpg，jpeg，png格式']]) ?>
                <input type="hidden" name="imgs" id="img2" value="">
                <?= \common\widgets\FormInput::widget(['name'=>'forminput|排序','extend'=>['name'=>'sort','type'=>'number','required'=>1,'value'=>0,'tips'=>'数值越大越靠前','info'=>$info]])?>
                <?= \common\widgets\FormInput::widget(['name'=>'formradio|状态','extend'=>['name'=>'status','info'=>$info]])?>
            </div>
            <div class="layui-tab-item" data-id="1">
                <table id="more-spec-lists-table-1" class="layui-table spec-lists-table">
                    <div class="layui-card-body">
                        <div class="layui-collapse like-layui-collapse" lay-accordion=""
                             style="border:1px dashed #c4c4c4">
                            <div class="layui-colla-item">
                                <h2 class="layui-colla-title like-layui-colla-title" style="background-color: #fff">操作提示<i
                                            class="layui-icon layui-colla-icon"></i></h2>
                                <div class="layui-colla-content layui-show" style="font-size: larger;">
                                    <p>*批发价将于分销佣金有关，分销佣金=（商品价格-批发价）*分销比例，当批发价大于商品价格，佣金将等于0</p>
                                    <p>*校区价将与校区佣金有关，校区佣金=校区价*校区抽成</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    <thead>
                    <tr style="background-color: #f3f5f9">
                        <th><span class="form-label-asterisk">*</span>规格名称</th>
                        <th><span class="form-label-asterisk">*</span>价格(元)</th>
                        <th>批发价(元)</th>
                        <th>校区价(元)</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
            <div class="layui-tab-item" data-id="2">
                <div class="form-group">
                    <label class="col-sm-2 control-label">商品详情：</label>
                    <div class="col-sm-9">
                        <?= \common\widgets\FormInput::widget(['name' => 'textarea', 'extend' => ['height' => '500', 'name' => 'details', 'class' => 'summernote_content hide','info'=>$info]]) ?>
                        <div class="summernote" data-place="" data-height="600px"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</form>
<script type="text/html" id="template-spec-1">
    <tr>
        <input type="hidden"  autocomplete="off" class="form-control" name="specAll[{i}][id]" value="{id}" required>
        <td>
            <input type="text"  autocomplete="off" class="form-control" placeholder="请输入名称" name="specAll[{i}][name]" value="{name}" required>
        </td>
        <td>
            <input type="number"  autocomplete="off" class="form-control" placeholder="请输入价格" name="specAll[{i}][price]" value="{price}" required>
        </td>
        <td>
            <input type="number"  autocomplete="off" class="form-control" placeholder="此价格与分销金额有关，如果不填默认为0" name="specAll[{i}][trade_price]" value="{trade_price}" required>
        </td>
        <td>
            <input type="number"  autocomplete="off" class="form-control" placeholder="请输入价格" name="specAll[{i}][ca_price]" value="{ca_price}" required>
        </td>
        <td>
            <a class="btn btn-success btn-xs add"><i class="fa fa-plus"></i> 新增</a>
            <a class="btn btn-danger btn-xs delete"><i class="fa fa-trash"></i> 删除</a>
        </td>
    </tr>
</script>
<?php $this->beginBlock('script'); ?>
<script>
    var elementtt;
    var currentType = -1;
    var formName = '<?=$addAll[0]['form']?>';
    var specAll = eval('<?=$this->params['specAll']?>');
    for (var i=0;i<specAll.length;++i){
        var onehtml = spectemplate(specAll[i],i)
        $('#more-spec-lists-table-1').find('tbody').append(onehtml)
    }
    function submitHandler() {
        if (true) {
            imgassign("input[name='imgarr1[]']",'#img1')
            imgassign("input[name='imgarr2[]']",'#img2')
            var sHTML = $('.summernote').summernote('code');
            $(".summernote_content").val(sHTML);
            $.operate.save(oesUrl, $('#form-dictdata-add').serialize(),errorJump);
        }
    }
    function errorJump(result) {
        if (result.code==-1){
            elementtt.tabChange('test1', result.data.id); //根据传入的id传入到指定的tab项
        }
    }
    layui.use('element', function () {
        elementtt = layui.element;
        elementtt.on('tab', function (elem) {
            formName = $(this).attr('data-formname')
        });
    });
    function spectemplate(data,i) {
        var html = $('#template-spec-1').html();
        var oReg =new RegExp(`{i}`,"g");
        html = html.replace(oReg, i)
        for (var j in data){
            var oReg =new RegExp(`{${j}}`,"g");
            html = html.replace(oReg, data[j])
        }
        return html;
    }
    $('body').on('click','.add',function () {
        var i = $(this).closest('tbody').find('tr').size();
        console.log(i)
        var html = spectemplate({name:'',price:'',trade_price:'',id:'',ca_price:''},i)
        $(this).closest('tbody').append(html)
    })
    $('body').on('click','.delete',function () {
        var i = $(this).closest('tbody').find('tr').size();
        if (i<=1){
            $.modal.msgError('第一个不允许删除');
            return
        }
        $(this).closest('tr').remove();
    })
</script>
<?php $this->endBlock(); ?>
